<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;

class Designs extends Model
{
    use SoftDelete;
    protected $deleteTime = 'deletetime';
    protected $hidden = ['deletetime', 'createtime', 'updatetime', 'birthday'];

    public function getAvatarAttr($value)
    {
        return $value ? request()->domain().$value : '';
    }

    public function circle()
    {
        return $this->hasMany(Circles::class, 'design_id', 'id');
    }

    public function service()
    {
        return $this->hasMany(DesignServices::class, 'design_id', 'id')->where('online', 'yes')->order('weigh desc');
    }

    public static function getDesigns($category_id)
    {
        $where = [
            'online' => ['>', 0]
        ];
        if ($category_id) {
            $where['category_id'] = $category_id;
        }
        return self::withCount('circle')->where($where)->order('weigh desc')->select();
    }

    public static function getById($designId)
    {
        return self::where('online', '>', 0)->find($designId);
    }

}
